<?php

/**
 * @desc Apllication start mode factory. 
 *
 */
abstract class Kernel_Mode_Factory 
{
    
    /**
     * @desc Return apllication start modes. 
     *
     * @return  Kernel_Mode_Abstract
     */
    static public function get() 
    {
        switch (true) {
            case self::_isCli():
                return new Kernel_Mode_Cli();
            case self::_isService():
                return new Kernel_Mode_Service();
            case self::_isServiceFile():
                return new Kernel_Mode_ServiceFile();
            default:
                return new Kernel_Mode_Html();
        }
    }

    /**
     * @desc Define whether application is started in CLI mode.
     *
     * @return  boolean
     */
    static private function _isCli() 
    {
        return !isset($_SERVER['SERVER_PROTOCOL']);
    }

    /**
     * @desc Define whether application is started in service mode. 
     *
     * @return  boolean
     */
    static private function _isService() 
    {
        return 'XMLHttpRequest' == @$_SERVER['HTTP_X_REQUESTED_WITH'];
    }
    
    /**
     * @desc Define whether application is started in service mode* 
     * * File upload compatible
     *
     * @return  boolean
     */
    static private function _isServiceFile() 
    {
        // get services query strings collection if one exists
        $services_path = SITE_ROOT . '/_includes/services.inc.php';
        $services = file_exists($services_path) 
            ? (array)require_once $services_path
            : array();
        
        return in_array($_SERVER['QUERY_STRING'], $services);
    }
    
}
